//
//  LoginViewController.m
//  Maintenance
//
//  Created by zhl on 2017/9/14.
//  Copyright © 2017年 com.kaopudian. All rights reserved.
//

#import "LoginViewController.h"
#import "UserInfoModel.h"
#import "RJTextField.h"


@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UIButton *sureBt;
@property (weak, nonatomic) IBOutlet RJTextField *phoneTF;

@property (weak, nonatomic) IBOutlet RJTextField *pwTF;


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KWhiteColor;

    ViewRadius(self.sureBt, 45*0.5);

    self.phoneTF.placeholder = @"手机号／会员ID";
    self.pwTF.placeholder = @"请输入密码";
    self.pwTF.textField.secureTextEntry = YES;
//    [self getToken];
    [[NSUserDefaults standardUserDefaults] setValue:@"123" forKey:@"userToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

//获取token
- (void)getToken{
    NSMutableDictionary * paramDict = [[NSMutableDictionary alloc] init];
    [paramDict setValue:@"000000" forKey:@"client_id"];
    [paramDict setValue:@"999999" forKey:@"client_secret"];
    [ZLHXHelper postURL:@"oauth/system/getToken" paramDict:paramDict completion:^(NSDictionary *responseObject) {
        
        
    } error:^(NSError *error) {
        
        [[NSUserDefaults standardUserDefaults] setValue:@"123" forKey:@"userToken"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }];
}

- (IBAction)sendAction:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    self.pwTF.textField.secureTextEntry = !sender.selected;
}

- (IBAction)sureAction:(id)sender {
    
    @WeakObj(self);
    if (self.phoneTF.textField. text.length == 0) {
        
        iToast *itoast = [[iToast alloc]initWithText:@"手机号不能为空"];
        [itoast show];
        return;

    }
    if (self.pwTF.textField.text.length == 0){
        
        iToast *itoast = [[iToast alloc]initWithText:@"请输入正确的登录密码"];
        [itoast show];
        return;

    }
    
    NSMutableDictionary * paramDict = [[NSMutableDictionary alloc] init];
    [paramDict setValue:self.phoneTF.textField. text forKey:@"loginNameOrTel"];
    [paramDict setValue:self.pwTF.textField.text forKey:@"password"];
    
    [self showHudInView:self.view hint:@"登录中..."];
    __weak typeof(self) weakSelf = self;
    [ZLHXHelper postURL:@"user/login" paramDict:paramDict completion:^(NSDictionary *responseObject) {
        
        [weakSelf hideHud];
        
        //登录成功返回uid设置(保存用户信息)
        //登录环信
        [ZLHXHelper loginHXWithPw:self.pwTF.textField.text];

        [self saveUserInfo:responseObject key:@"obj"];

        //把密码存起来
        [[NSUserDefaults standardUserDefaults] setValue:self.pwTF.textField.text forKey:@"passWord"];
        [[NSUserDefaults standardUserDefaults] synchronize];
       
        [selfWeak dismissViewControllerAnimated:YES completion:nil];
        
    } error:^(NSError *error) {
        
        
         [self showHudInView:self.view hint:error.localizedDescription];
//        iToast *itoast = [[iToast alloc]initWithText:error.localizedDescription];
//        [itoast show];
        
        
    }];
    
}
#pragma  mark - private

//保存用户信息
- (void)saveUserInfo:(NSDictionary *)infoDict key:(NSString *)key{
    
    NSDictionary *info = infoDict[key];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:info];
    [[NSUserDefaults standardUserDefaults] setValue:data forKey:@"userInfoModal"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    UserInfoModel *userModal = [UserInfoModel mj_objectWithKeyValues:info];
    
    [DataCenter shareInstence].companyList = userModal.companyList;
    
    [DataCenter shareInstence].mobile = userModal.mobile;
    [DataCenter shareInstence].loginName = userModal.loginName;
    [DataCenter shareInstence].name = userModal.name;
//    for (CompanyModel *companyModal in userModal.companyList) {
//
//        [DataCenter shareInstence].companyId = companyModal.companyId;
//        [DataCenter shareInstence].companyName = companyModal.name;
//
//    }
    
    
    
   //QOS_CLASS_UTILITY  实用工具(耗时操作，可以使用这个选项)
   NSMutableDictionary * paramDict = [[NSMutableDictionary alloc] init];
   
    //根据公司获取所有用户
//
        NSMutableArray *allUserInfo = [NSMutableArray array];
    
    for (int i=0; i<userModal.companyList.count; i++) {
            CompanyModel *companyModal = userModal.companyList[i];
            [paramDict setValue:companyModal.companyId forKey:@"companyId"];
            [ZLHXHelper postURL:@"user/selectUserByCompanyId" paramDict:paramDict completion:^(NSDictionary *responseObject) {
                NSArray *arr = [responseObject objectForKey:@"obj"];
                [allUserInfo addObjectsFromArray:arr];
                
                if (i == userModal.companyList.count-1) {
                    NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:allUserInfo];
                    
                    [[NSUserDefaults standardUserDefaults] setValue:userData forKey:@"numberArr"];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                }
                
                
            } error:^(NSError *error) {
                
                //        iToast *itoast = [[iToast alloc]initWithText:error.localizedDescription];
                //        [itoast show];
                
            }];
            
            
            
        }
        
    
//    dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{
    
    
        
//    });
  
}








@end
